package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.lock.CharItemPowerReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemName;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1ItemPower_name;
import com.lineage.server.utils.log.PlayerLogUtil;
import java.sql.Timestamp;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class takePower_hole2 extends ItemExecutor {
  private int type;
  
  private static final Log _log = LogFactory.getLog(takePower_hole2.class);
  
  private static final Random _random = new Random();
  
  public static ItemExecutor get() {
    return new takePower_hole2();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    try {
      int targObjId = data[0];
      L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
      if (tgItem == null)
        return; 
      if (tgItem.get_power_name() == null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fT这个物品没有凹槽!"));
        return;
      } 
      if (tgItem.isEquipped()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR你必须先解除物品装备!"));
        return;
      } 
      L1ItemPower_name power = tgItem.get_power_name();
      int use_type = tgItem.getItem().getUseType();
      boolean isErr = false;
      boolean isErr2 = false;
      if (power.get_hole_2() == 0) {
        switch (use_type) {
          case 2:
          case 19:
          case 20:
          case 21:
          case 22:
            isErr = true;
            break;
        } 
        switch (use_type) {
          case 1:
            isErr2 = true;
            break;
        } 
        if (isErr)
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("该防具第2洞尚未插入卡片")); 
        if (isErr2)
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("该武器第2洞尚未插入卡片")); 
        return;
      } 
      if (power.get_hole_2() > 0 && power.get_hole_count() >= 1) {
        if (_random.nextInt(100) + 1 <= this.type) {
          pc.getInventory().storeItem(power.get_hole_2(), 1L);
          power.set_hole_2(0);
          pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
          pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
          CharItemPowerReading.get().updateItem(tgItem.getId(), tgItem.get_power_name());
          打洞水晶拔出成功("玩家 :" + pc.getName() + "||" + item.getName() + "成功拔出.第1洞 ,时间:" + 
              new Timestamp(System.currentTimeMillis()) + ")");
        } else {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("取回失败"));
        } 
        pc.getInventory().removeItem(item, 1L);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this.type = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
  }
  
  public static void 打洞水晶拔出成功(String info) {
    PlayerLogUtil.writeLog("打洞水晶拔出成功", info);
  }
}
